local kejie = fk.CreateSkill{
  name = "lingling__kejie",
}

Fk:loadTranslationTable{
  ["lingling__kejie"] = "克捷",
  [":lingling__kejie"] = "一名角色回合结束时，你可以弃置一张【杀】，然后选择：重置其一个限定技，若不为你，你失去1点体力"..
  "（每名其他角色限重置一次）；获得其手牌和装备区各一张牌。若其没有限定技，装备区也没有牌，则改为你对其造成1点伤害。",

  ["#lingling__kejie-invoke"] = "克捷：弃置一张【杀】，根据 %dest 有无限定技和装备执行选项",
  ["#lingling__kejie-choice"] = "克捷：选择对 %dest 执行的一项",
  ["lingling__kejie1"] = "重置其一个限定技",
  ["lingling__kejie2"] = "获得其手牌和装备区各一张牌",
  ["#lingling__kejie-limited"] = "克捷：选择令 %dest 重置的限定技",
}

local U = require "packages/utility/utility"

kejie:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(kejie.name) and not player:isKongcheng() and not target.dead
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = kejie.name,
      pattern = "slash",
      prompt = "#lingling__kejie-invoke::"..target.id,
      cancelable = true,
      skip = true,
    })
    if #card > 0 then
      event:setCostData(self, {tos = {target}, cards = card})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards, kejie.name, player, player)
    if player.dead or target.dead then return end
    local choices, yes = {}, true
    local skills = table.filter(target:getSkillNameList(), function (s)
      return Fk.skills[s]:hasTag(Skill.Limited)
    end)
    if #skills > 0 then
      yes = false
      skills = table.filter(skills, function (s)
        return target:usedSkillTimes(s, Player.HistoryGame) > 0
      end)
      if #skills > 0 and (target == player or not table.contains(player:getTableMark(kejie.name), target.id)) then
        table.insert(choices, "lingling__kejie1")
      end
    end
    if #target:getCardIds("e") > 0 then
      yes = false
      table.insert(choices, "lingling__kejie2")
    end
    if yes then
      room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = kejie.name,
      }
    else
      if not target:isKongcheng() and target ~= player then
        table.insertIfNeed(choices, "lingling__kejie2")
      end
      if #choices > 0 then
        local choice = room:askToChoice(player, {
          choices = choices,
          skill_name = kejie.name,
          prompt = "#lingling__kejie-choice::"..target.id,
          all_choices = {"lingling__kejie1", "lingling__kejie2"},
        })
        if choice == "lingling__kejie1" then
          choice = room:askToChoice(player, {
            choices = skills,
            skill_name = kejie.name,
            prompt = "#lingling__kejie-limited::"..target.id,
          })
          target:setSkillUseHistory(choice, 0, Player.HistoryGame)
          if target ~= player then
            room:addTableMark(player, kejie.name, target.id)
            room:loseHp(player, 1, kejie.name)
          end
        else
          local cards = U.askforCardsChosenFromAreas(player, target, "he", kejie.name, nil, nil, false)
          if #cards > 0 then
            room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, kejie.name, nil, false, player)
          end
        end
      end
    end
  end,
})

return kejie
